package top.lgd.utils;

import cn.hutool.core.collection.CollectionUtil;
import top.lgd.pojo.res.TreeRes;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/22 9:58
 */
public class TreeUtils {
    public static List<TreeRes> genTree(List<TreeRes> treeResList, Map<Long, List<TreeRes>> parentMap) {
        if (CollectionUtil.isNotEmpty(treeResList)) {
            treeResList.forEach(p -> {
                List<TreeRes> childrenList = parentMap.get(p.getId());
                if (CollectionUtil.isNotEmpty(childrenList)) {
                    childrenList.stream().peek(c -> c.setChildren(parentMap.get(c.getId()))).collect(Collectors.toList());
                    p.setChildren(childrenList.stream().filter(c -> p.getId().equals(c.getParentid())).collect(Collectors.toList()));
                }
            });
        }
        return treeResList;
    }
}
